package com.zjw.zy.componse.demo.ext

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.annotation.*
import androidx.lifecycle.LiveData
import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.zjw.zy.componse.demo.R
import com.zjw.zy.componse.demo.base.BaseComposeFragment

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/26 17:15
 * @description：
 */


fun BaseComposeFragment.go(@IdRes id: Int, args: Bundle? = null, singTop: Boolean = false) {
    findNavController().navigate(id, args, NavOptions.Builder().apply {
        this.setLaunchSingleTop(singTop)
    }.build())
}


fun BaseComposeFragment.goSingleTask(@IdRes id: Int, args: Bundle? = null) {
    val nav = findNavController()
    nav.navigate(id, args, NavOptions.Builder().apply {
        this.setLaunchSingleTop(true)
        this.setPopUpTo(id, false)
    }.build())
}

fun BaseComposeFragment.goForResult(
    @IdRes id: Int,
    args: Bundle? = null,
    singTop: Boolean = false
): LiveData<Any?> {
    val nav = findNavController()
    val live =
        nav.currentBackStackEntry!!.savedStateHandle.getLiveData<Any?>(nav.currentBackStackEntry!!.destination.id.toString())
    go(id, args, singTop)
    return live
}

fun BaseComposeFragment.backWithResult(payload: Any?) {
    val nav = findNavController()
    nav.previousBackStackEntry?.let {
        it.savedStateHandle.getLiveData<Any?>(it.destination.id.toString()).postValue(payload)
    }
    nav.navigateUp()
}

@SuppressLint("RestrictedApi")
fun BaseComposeFragment.popWithResult(@IdRes id: Int, payload: Any?) {
    val nav = findNavController()
    nav.backStack.apply {
        val iterator = this.descendingIterator()
        while (iterator.hasNext()) {
            val next = iterator.next()
            if (next.destination.id == id) {
                next.savedStateHandle.getLiveData<Any?>(id.toString()).postValue(payload)
                break
            }
        }
    }
    nav.popBackStack(id, false)
}


fun BaseComposeFragment.goWithRightAnim(
    @IdRes destination: Int,
    args: Bundle? = null,
    singTop: Boolean = false,
    @AnimRes @AnimatorRes enter: Int = R.anim.default_enter_anim,
    @AnimRes @AnimatorRes exit: Int = -1,
    @AnimRes @AnimatorRes popEnter: Int = -1,
    @AnimRes @AnimatorRes popExit: Int = R.anim.default_exit_anim,
) {
    findNavController().navigate(
        destination, args,
        NavOptions.Builder().setEnterAnim(enter)
            .setExitAnim(exit)
            .setPopEnterAnim(popEnter)
            .setPopExitAnim(popExit)
            .setLaunchSingleTop(singTop)
            .build()
    )
}

fun BaseComposeFragment.goWithTopAnim(
    @IdRes destination: Int,
    args: Bundle? = null,
    singTop: Boolean = false,
    @AnimRes @AnimatorRes enter: Int = R.anim.default_enter_y_anim,
    @AnimRes @AnimatorRes exit: Int = -1,
    @AnimRes @AnimatorRes popEnter: Int = -1,
    @AnimRes @AnimatorRes popExit: Int = R.anim.default_exit_y_anim,
) {
    findNavController().navigate(
        destination, args,
        NavOptions.Builder().setEnterAnim(enter)
            .setExitAnim(exit)
            .setPopEnterAnim(popEnter)
            .setPopExitAnim(popExit)
            .setLaunchSingleTop(singTop)
            .build()
    )
}

fun BaseComposeFragment.back() {
    findNavController().navigateUp()
}

fun BaseComposeFragment.finish() {
    requireActivity().finish()
}